/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.cassandra.db.compression;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class CompressionDictionaryTrainingConfigTest
{
    @Test
    public void testBuilderDefaults()
    {
        CompressionDictionaryTrainingConfig config = CompressionDictionaryTrainingConfig.builder().build();

        assertThat(config.maxDictionarySize)
        .as("Default max dictionary size should be 64KB")
        .isEqualTo(65536);
        assertThat(config.maxTotalSampleSize)
        .as("Default max total sample size should be 10MB")
        .isEqualTo(10 * 1024 * 1024);
        assertThat(config.samplingRate)
        .as("Default sampling rate should be 100 (1%)")
        .isEqualTo(100);
    }

    @Test
    public void testCalculatedThresholds()
    {
        int dictSize = 16 * 1024; // 16KB
        int sampleSize = 2 * 1024 * 1024; // 2MB
        float samplingRate = 0.005f; // 0.5%

        CompressionDictionaryTrainingConfig config = CompressionDictionaryTrainingConfig.builder()
                                                                                        .maxDictionarySize(dictSize)
                                                                                        .maxTotalSampleSize(sampleSize)
                                                                                        .samplingRate(samplingRate)
                                                                                        .build();

        // Verify all calculated values are consistent
        assertThat(config.maxDictionarySize).isEqualTo(dictSize);
        assertThat(config.maxTotalSampleSize).isEqualTo(sampleSize);
        assertThat(config.acceptableTotalSampleSize).isEqualTo(sampleSize / 10 * 8);
        assertThat(config.samplingRate).isEqualTo(Math.round(1 / samplingRate));

        // Verify relationship between max and acceptable sample sizes
        assertThat(config.acceptableTotalSampleSize)
        .as("Acceptable sample size should be less than or equal to max")
        .isLessThanOrEqualTo(config.maxTotalSampleSize);
    }
}
